//
//  HEMyReportViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMyReportViewController.h"
#import "HEMyReportCell.h"
#import "HECheckReportModel.h"
#import "HEReadPDFViewController.h"
#import "HESpPDFModel.h"

@interface HEMyReportViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HECheckReportModel *> * dataSourceArr;
 
@end

@implementation HEMyReportViewController

- (NSMutableArray<HECheckReportModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的报告";
    _pageNum = 1;
    _pageSize = 20;

    [self initUI];

    [self loadData];
    [self setCustomTableView:_tableView];
}


- (void)initUI {
 
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight ) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];

    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];

    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = NO;
        weakSelf.pageNum += 1;
        [weakSelf loadData];
    }];

    [_tableView registerClass:[HEMyReportCell class] forCellReuseIdentifier: KHEMyReportCell];
}

#pragma mark-

- (void)loadData {
    
    HEPageRequest *params = [HEPageRequest new];
    params.pageNum = _pageNum;
    params.pageSize = _pageSize;
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KcheckReport parameters:params success:^(id  _Nonnull responseObject) {

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEPageResponse *response = [HEPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HECheckReportModel mj_objectArrayWithKeyValuesArray:response.data.list]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.list.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEMyReportCell *cell = [tableView dequeueReusableCellWithIdentifier: KHEMyReportCell];
    [cell configCellWithModel: _dataSourceArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHEMyReportCell cacheByIndexPath:indexPath configuration:^(HEMyReportCell *cell) {
        [cell configCellWithModel: self.dataSourceArr[indexPath.row]];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HECheckReportModel *model = _dataSourceArr[indexPath.row];
    [self loadPDFUrl: model];
}

- (void)loadPDFUrl:(HECheckReportModel *)model {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KSpinalDetail parameters:@{@"sn" : model.sn} success:^(id  _Nonnull responseObject) {

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        NSArray <HESpPDFModel *> *pdfs = [HESpPDFModel mj_objectArrayWithKeyValuesArray:response.data];
     
        if (response.success) {
            if (pdfs.count > 0) {
                HEReadPDFViewController *vc = [HEReadPDFViewController new];
                vc.urlStr = pdfs.lastObject.files;
                [self.navigationController pushViewController:vc animated:YES];
            }else {
                [Dialog toastBottom:@"暂无报告"];
            }
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

@end
